package com.atguigu.tingshu.order.strategy.impl;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;

import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.strategy.SaveOrderStrategy;
import com.atguigu.tingshu.order.template.SaveOrderTemplate;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class WeiXinSaveOrderStrategy extends SaveOrderTemplate implements SaveOrderStrategy {
    private final OrderInfoMapper orderInfoMapper;
    private final OrderDetailMapper orderDetailMapper;
    private final OrderDerateMapper orderDerateMapper;
    private final RedissonClient redissonClient;

    public WeiXinSaveOrderStrategy(OrderInfoMapper orderInfoMapper, OrderDetailMapper orderDetailMapper, OrderDerateMapper orderDerateMapper, RedissonClient redissonClient) {
        this.orderInfoMapper = orderInfoMapper;
        this.orderDetailMapper = orderDetailMapper;
        this.orderDerateMapper = orderDerateMapper;
        this.redissonClient = redissonClient;
    }
    /**
     * @description: 定义保存订单
     * @author Petrichor
     * @date 2024/1/9 18:39
     * @param orderInfoVo 订单对象
     * @return
    **/
    @Override
    public OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) {
        // UUID 生成 订单编号
        String orderNo = UUID.randomUUID().toString().replace("-", "");
        // 保存订单相关数据  &&  发送超时未支付自动取消订单的延迟消息
        this.saveOrderAndDetailAndOrderDerate(orderInfoVo, orderNo);

        return orderInfoVo;
    }
    /**
     * @description: 发送超时未支付自动取消订单的延迟消息
     * @author Petrichor
     * @date 2024/1/8 21:21
     * @param orderNo 订单编号
    **/
    @Override
    public void saveOrderAfterProcess(String orderNo) {
        sendDelayMessage(orderNo);
    }

    private void sendDelayMessage(String orderNo) {
        try {
            // 获取阻塞队列
            RBlockingDeque<Object> orderCancelBlockingDeque = redissonClient.getBlockingDeque(KafkaConstant.QUEUE_ORDER_CANCEL);
            // 创建延迟队列
            RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(orderCancelBlockingDeque);
            // 发送延迟消息
            delayedQueue.offer(orderNo, KafkaConstant.DELAY_TIME, TimeUnit.SECONDS);
            log.info("添加延时队列成功 ，延迟时间：{}，订单id：{}", KafkaConstant.DELAY_TIME, orderNo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加延时队列失败 ，延迟时间：{}，订单id：{}", KafkaConstant.DELAY_TIME, orderNo);
        }
    }

    @Override
    public String getPayWay() {
        // 微信支付
        return SystemConstant.ORDER_PAY_WAY_WEIXIN;
    }


}